﻿using LambdaToSql.FrameWork;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Reflection.Emit;
using System.Text;

namespace LambdaToSql.Realization.SqLite
{
    internal partial class Queryable<T> : LambdaToSql.Realization.Queryable<T> where T : class, new()
    {
        public Queryable(LambdaToSql.EntityModel.DbContext context) : base(context)
        {

        }
        public override IList<T> ToPageList(int pageIndex, int pageSize, ref int total)
        {
            var DbHelper = this.Context.DbHelper;

            var totalSql = string.Format("select Count(*) from {0} {1}", this.Entity.TableName, this.Entity.WhereStr);
            total = Convert.ToInt32(DbHelper.ExecuteScalar(totalSql, this.Entity.Parameters.ToArray()));

            var sql = string.Format(this.Entity.PageTemplate, pageSize, pageIndex - 1);
            var sdr = DbHelper.ExecuteTable(sql, this.Entity.Parameters.ToArray());

            var list = Tools.DataTableToList<T>(sdr);
            return list;
        }
    }
}
